package br.com.rookgraph.utils;

import br.com.rookgraph.Edge;

/**
 * Metodos utilitarios para trabalhar com Arrays.
 *
 * @author Luan Pontes
 *
 */
public class ArrayUtils {

	/**
	 * Verifica se o contem o elemento na lista.
	 *
	 * @param v
	 * @param list
	 * @return
	 */
	public static boolean containsList(int ele, int[] list) {
		for (int e : list) {
			if(e == ele) return true;
		}
		return false;
	}

	/**
	 * Retorna a proxima posição livre de
	 * um array de inteiros.
	 * @param list
	 * @return
	 */
	public static int nextFree(int[] list){
		int i;
		for ( i = 0; i < list.length; i++) {
			if(list[i] == 0){
				return i;
			}
		}
		return i;
	}

	public static int[] copyNewArray(int[] listCut, int length) {
		int[] newListCut = new int[length];
		System.arraycopy(listCut, 0, newListCut, 0, length);
		return newListCut;
	}

	public static Edge[] removeEdgeList(Edge arestCut, final Edge[] list) {
		int count = 0;
		for (int i = 0; i < list.length; i++) {
			if (list[i].equals(arestCut)) {
				list[i] = null;
				count++;
				for (int j = i; j < list.length - 1; j++) {
					list[j] = list[j + 1];
				}
			}
		}
		int length = list.length - count;
		Edge[] newList = new Edge[length];
		System.arraycopy(list, 0, newList, 0, length);
		return newList;
	}

	public static boolean contains(Edge candidata, Edge[] edges) {

		for (Edge aresta : edges) {
			if (aresta != null && aresta.equals(candidata))
				return true;
		}

		return false;
	}


}
